package logic

import (
	"context"
	"gozerogateway/common/errorx"
	"gozerogateway/rpc/common"

	"gozerogateway/rpc/market/internal/svc"
	"gozerogateway/rpc/market/marketclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DashServiceStatLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDashServiceStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DashServiceStatLogic {
	return &DashServiceStatLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DashServiceStatLogic) DashServiceStat(in *marketclient.DashServiceStatReq) (*marketclient.DashServiceStatResp, error) {
	// todo: add your logic here and delete this line

	list := []*marketclient.DashServiceStatItemResp{}
	all, err := l.svcCtx.SvcModel.GroupByLoadType(l.ctx)
	if err != nil {
		return nil, errorx.NewDefaultError("grouploadtype error")
	}
	for _, restemp := range *all { //*
		listitem := &marketclient.DashServiceStatItemResp{
			Name:  restemp.Name,
			Value: restemp.Value,
		}
		list = append(list, listitem)
	}

	legend := []string{} //empty []
	for index, item := range list {
		name, ok := common.LoadTypeMap[item.LoadType]
		if !ok {
			return nil, errorx.NewDefaultError("loadtype not found")
		}
		list[index].Name = name
		legend = append(legend, name)
	}
	return &marketclient.DashServiceStatResp{
		Data:   list,
		Legend: legend,
	}, nil
}
